package Compute;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
class Window extends JFrame{
    JTextField inputOne,inputTwo;
    JComboBox fuhao;
    JTextArea show;
    JButton button;
    OperatorListener operater;//选择符号
    ComputerListener computer;
    public Window(){
        init();
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public void init() {
        setLayout(new FlowLayout());//设置流程布局
        inputOne = new JTextField(5);
        inputTwo = new JTextField(5);
        fuhao = new JComboBox();
        button = new JButton("计算");
        show = new JTextArea(9, 30);
        fuhao.addItem("选择运算符号：");
        String [] a = new String[]{"+", "-", "*", "/"};
        for (int i = 0; i < a.length; i++) {
            fuhao.addItem(a[i]);
        }
        operater = new OperatorListener();//选择符号
        computer = new ComputerListener();//
        operater.setJComboBox(fuhao);
        operater.setWork(computer);
        computer.setOne(inputOne);
        computer.setTwo(inputTwo);
        computer.setArea(show);
        fuhao.addItemListener(operater);//添加子选项监视器
        button.addActionListener(computer);//为符号添加活动监视器
        add(inputOne);
        add(fuhao);
        add(inputTwo);
        add(button);
        add(new JScrollPane(show));
    }
}
class OperatorListener implements ItemListener {

    JComboBox choice;
    ComputerListener work;
    public void setJComboBox(JComboBox comboBox){
        this.choice=comboBox;
    }
    public void setWork(ComputerListener compute){
        this.work=compute;
    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        String fuhao=choice.getSelectedItem().toString();
        work.setFuhao(fuhao);
    }
}
class ComputerListener implements ActionListener {
    JTextField one,two;
    JTextArea area;
    String fuhao;
    public void setOne(JTextField oen){
        this.one=one;
    }
    public void setTwo(JTextField two){
        this.two=two;
    }
    public void setFuhao(String fuhao){
        this.fuhao=fuhao;
    }
    public void setArea(JTextArea area) {
        this.area = area;
    }
    @Override
    public void actionPerformed(ActionEvent event){
        try{
            double number1=Double.parseDouble(one.getText());
            double number2=Double.parseDouble(two.getText());
            double result =0;
            if (fuhao.equals("-")){
                result=number1-number2;
            }else if (fuhao.equals("+")){
                result=number1+number2;
            }else if (fuhao.equals("*")){
                result  =number1*number2;
            }else if (fuhao.equals("/")){
                result=number1/number2;
            }
            area.append(number1+" "+fuhao+" "+number2+"="+result+"\n");
        }catch (Exception e){
            area.append("请输入数字\n");
        }
    }
}
public class WindowOperation extends JFrame {
    public static void main(String[] args) {
        Window window=new Window();
        window.setBounds(200,200,500,300);
        window.setTitle("计算器");
    }

}
